package com.kewill.jira.servlet;

import com.kewill.jira.common.DaoAnnotation;
import com.kewill.jira.common.JiraAttribute;
import com.kewill.jira.common.TARGenerator;
import com.kewill.jira.dao.IIssueDao;
import com.kewill.jira.dao.IssueDaoImpl;
import com.kewill.jira.model.Issue;
import com.kewill.jira.util.ServletUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;

/**
 * Created by YanJun on 2016/7/4.
 */
public class IssueTarDownloadServlet extends IssueBaseServlet {
    private IIssueDao issueDao;
    private Logger tarDownloadLog = LoggerFactory.getLogger(IssueTarDownloadServlet.class);

    @DaoAnnotation("IssueDaoImpl")
    public void setIssueDao(IIssueDao issueDao) {
        this.issueDao = issueDao;
    }

    public void downloadTar(HashMap<String,Object> servletMetaMap)
            throws ServletException, IOException {
        tarDownloadLog.debug("invoke IssueShowServlet.downloadTar()");
        HttpServletRequest request = (HttpServletRequest)servletMetaMap.get(JiraAttribute.PARAM_REQUEST);
        HttpServletResponse response = (HttpServletResponse)servletMetaMap.get(JiraAttribute.PARAM_RESPONSE);
        String issueId = ServletUtil.getParam(request,"id");
        Issue issue = issueDao.loadIssueById(new Long(issueId));
        TARGenerator tar = new TARGenerator(issue);
        try {
            File newTarFile = tar.doExecute();
            ServletUtil.doDownload(newTarFile,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
